package org.fxp.login;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.Query;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.data.extensions.Name;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class UpdateBirthday {
	public void inputBirthday(List contacts) {
		// Get all contacts

		// Login Gmail
		try {
			ContactsService myService = new ContactsService("contact-1");
			myService.setUserCredentials("username@gmail.com", "password");
			URL feedUrl = new URL(
					"http://www.google.com/m8/feeds/contacts/fxplog@gmail.com/full");
			Query myQuery = new Query(feedUrl);
			ContactFeed resultFeed = myService
					.query(myQuery, ContactFeed.class);

			// Print the results
			for (int i = 0; i < resultFeed.getEntries().size(); i++) 
			{
				ContactEntry entry = resultFeed.getEntries().get(i);
				// We assume existence of a nameToPrintableString function that
				// formats
				// the Name of a contact into a string suitable for printing.if (entry.hasName()) {
				Name name = entry.getName();
				
				if (name!=null&&name.hasFullName())
				{
			        String fullNameToDisplay = name.getFullName().getValue();
			        
			        for(int j=0;j<contacts.size();j++)
			        {
			        	myContact c=(myContact)contacts.get(j);
				        if(fullNameToDisplay.equals(c.name))
				        {
				        	entry.update();
							entry.setBirthday(c.bd);
							URL editUrl = new URL(entry.getEditLink().getHref());
							myService.update(editUrl, entry);
							System.out.println("Upload contact "+ fullNameToDisplay);
							contacts.remove(j);
				        	entry.update();
				        	break;
				        }
			        }
				}
			}
			for(int j=0;j<contacts.size();j++)
			{
				myContact c=(myContact)contacts.get(j);
				System.out.println("Left contact "+ c.name);
			}
		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Check if the contact exist
		// If the name exist update the birthday

	}
}
